﻿<!--#include file="inc/common.asp" -->
<%
'保存评论信息的功能模块
'
'author: evan zhao
'
  if not userRightObj.canReply then
    message("您没有权限发表评论")
  end if

  if blogCatObj.isRSS then
    message("本文内容从RSS源自动获取，无法留言")
  end if

  call openDB
  
  dim actionType,content_id,parent_id, comment_id
  dim title, detail, insert_time, user_id, user_name, e_mail, web_url,  is_secret
  dim     blogItem

  content_id = clng(request("content_id"))
  if content_id="" then
    message("文章不存在")
    return
  else
    content_id=clng(content_id)  
  end if
  set blogItem = new BlogItemClass
  if  blogItem.load(content_id) = false then
    message("文章不存在")
    call closeDB()
    response.end
  end if
  
  
  actionType = request("actionType")
  parent_id = request("parent_id")
  if not isEmpty(parent_id) and parent_id<>"" then
    parent_id = clng(parent_id)
    if parent_id=0 then
      parent_id = Empty
    end if
  end if
  
  comment_id = request("comment_id")
  if not isEmpty(comment_id) and comment_id<>"" then
    comment_id = clng(comment_id)
  end if
  
  dim rs2 
  if (actionType="doSave") then
  
     if isEmpty(userObj.userId) and not IsEmpty(session("MISSLOG_CHECK_NUM")) and session("MISSLOG_CHECK_NUM") <> request("check_num") then
        message("校验码输入错误")
     end if
  
    'todo判断是否重复刷新

    detail = request("detail")
    if (request("use_rte")<>"true") then
        detail = filterHTML2(detail, true)
    else    
        detail = filterHTML2(detail, false)
    end if    
    user_name = userObj.userName
    e_mail = userObj.email
    web_url = userObj.webUrl
    user_id = userObj.userId
    if isEmpty(user_id) or user_id="" then
        if trim(user_name)="" then
            user_name = "[匿名]"
        else
            'user_name = user_name & "[未注册用户]" 
        end if
    end if    
    is_secret = false
    if request("is_secret")="true" then
        is_secret = true
    end if
    
    'sql = " insert into blog_comments(blog_id, content_id, detail, user_name, email, web_url, user_id, is_secret) " &_
    '      "    values( " & blogId & ", " & content_id & ", '" & detail & "', '" & user_name & "', '" & e_mail & "', '" & web_url &"', " & user_id & ", " & is_secret & ") "                 
    'conn.execute(sql)

    set rs2 = Server.CreateObject("ADODB.Recordset")
    
    if comment_id="" then '新增
        comment_id=0
    else
        comment_id=clng(comment_id)    
    end if
    sql = " select comment_id, blog_id, content_id,parent_id, detail, user_name,email,web_url,face,user_id, is_secret from blog_comments where comment_id=" & comment_id
    rs2.open sql, conn, 1, 3
    if rs2.recordCount<=0 then
        rs2.addNew()
    end if
    rs2("blog_id") = blogObj.blogId
    rs2("content_id") = content_id
    if isNumeric(parent_id) then
        rs2("parent_id") = parent_id
    else
        rs2("parent_id") = Null
    end if    
    rs2("detail") = detail
    rs2("user_name") = user_name
    rs2("email") = e_mail
    rs2("web_url") = web_url
    if not isNull(userObj.faceUrl) and not isEmpty(userObj.faceUrl) and userObj.faceUrl<>"" then
        rs2("face") = userObj.faceUrl
    end if
    if user_id<>"" then
        rs2("user_id") = user_id
    end if    
    rs2("is_secret") = is_secret
    rs2.update()

    if  comment_id=0 then   
        sql = " update blog_content set reply_count=reply_count+1, update_time=now() where content_id=" & content_id
        conn.execute(sql)
    end if
    
    comment_id = rs2("comment_id")
    rs2.close()
    set rs2 = nothing
    
    call blogItem.reload(content_id)  
  
    call blogObj.loadTopCommentsFromDB()
    call blogObj.cache()
    
    assertConnected()
    closeDB()
    response.clear
    call redirect("detail.asp?blog_id=" & blogObj.blogId & "&cat_id=" & blogCatObj.catId & "&content_id=" & content_id & "&#comment" & comment_id)
    response.end
    
  elseif actionType="doDelete" and comment_id<>"" and userObj.userId<>""  then
    sql = " select comment_id,user_id from blog_comments where comment_id=" & comment_id & " and content_id="&content_id
    set rs2 = Server.CreateObject("ADODB.Recordset")
    rs2.open sql,conn,1,3
    if not rs2.bof and not rs2.eof  then
      if rs2("user_id")=userObj.userId or userRightObj.canDelete then
        rs2.delete()
        sql = " update blog_content set reply_count=reply_count-1, update_time=now() where content_id=" & content_id
        conn.execute(sql)
      end if  
    end if
    rs2.close()
    set rs2 = nothing

    call blogItem.reload(content_id)  
  
    call blogObj.loadTopCommentsFromDB()
    call blogObj.cache()

    assertConnected()
    closeDB()
    response.clear
    call redirect("detail.asp?blog_id=" & blogObj.blogId & "&cat_id=" & blogCatObj.catId & "&content_id=" & content_id )
    response.end
  else  
    closeDB()
    response.clear
    call transfer("detail.asp")
  end if



%>

<!--#include file="inc/end.asp" -->